<?php 
/**
 * 商品控制列表
 */
class GoodsController extends AuthController{
	private $model;
	public function __init(){
		parent::__init();
		$this->model = K('Goods');
	}
	
	public function index(){
		$data = $this->model->all();
		$this->assign('data',$data);
		
		$this->display();
	}
	/**
	 * 编辑商品
	 */
	public function edit(){
		//获取gid
		$gid = Q('get.gid',0,'intval');
		if(IS_POST){
			if(!$this->model->edit($gid)) $this->error($this->model->error);
			$this->success('编辑成功');
		}
		
		//获取商品
		$dataGoods = $this->model->where("gid={$gid}")->find();
		$this->assign('dataGoods',$dataGoods);
		//获取分类
		$dataCate = K('Category')->all();
		$dataCate = Data::tree($dataCate, 'cname');
		$this->assign('dataCate',$dataCate);
		//获取品牌
		$dataBrand = K('Brand')->all();
		$this->assign('dataBrand',$dataBrand);
		//获取属性(页面中需要哪些数据，已什么形式存在，可以在这里进行数组重组)
		$dataAttr = K('GoodsAttr')->where("goods_gid={$gid} AND class=1")->all();
		foreach ($dataAttr as $k => $v) {
			$taid = $v['type_attr_taid'];
			$type = K('TypeAttr')->where("taid={$taid}")->find();
			$dataAttr[$k]['tavalue'] = explode(',', $type['tavalue']);
			$dataAttr[$k]['taname'] = explode(',', $type['taname']);
		}
		$this->assign('dataAttr',$dataAttr);
		//获取规格
		$dataSpec = K('GoodsAttr')->where("goods_gid={$gid} AND class=0")->all();
		foreach ($dataSpec as $k => $v) {
			$taid = $v['type_attr_taid'];
			$type = K('TypeAttr')->where("taid={$taid}")->find();
			$dataSpec[$k]['tavalue'] = explode(',', $type['tavalue']);
			$dataSpec[$k]['taname'] = explode(',', $type['taname']);
		}
//		print_r($dataSpec);die;
		$this->assign('dataSpec',$dataSpec);
		//获取图册
		$gDetail = K('GoodsDetail')->where("goods_gid={$gid}")->find();
		$picture = explode(',', $gDetail['bigpic']);
		$this->assign('picture',$picture);
		$this->assign('gDetail',$gDetail);
		
		$this->display();
	}
	
	/**
	 * 添加商品
	 */
	public function add(){
		//获取所有分类
		$cate = K('Category')->all();
		$cate = Data::tree($cate, 'cname');
		$this->assign('cate',$cate);
		//获取所有商品
		$brand = K('Brand')->all();
		$this->assign('brand',$brand);
		
		if(IS_POST){
			if(!$this->model->addGoods()) $this->error($this->model->error);
			$this->success('添加成功');
		}
		
		
		
		$this->display();
	}
	
	
	/**
	 * 删除商品
	 */
	public function delete(){
		$gid = Q('get.gid',0,'intval');
		//删除商品表数据
		$dataGoods = $this->model->where("gid={$gid}")->find();
		//删除表中的图片(Controller 真正执行的路径是在index.php中，所有unlink是index中相对路径)
		unlink($dataGoods['picture']);
//		print_r($dataGoods);die;
		$this->model->where("gid={$gid}")->delete();
		//删除商品属性表
		K('GoodsAttr')->where("goods_gid={$gid}")->delete();
		//删除商品详情表
		$dataDetail = K('GoodsDetail')->where("goods_gid={$gid}")->find();
		//删除图片
		//删除小图
		$smallpic = explode(',', $dataDetail['smallpic']);
		foreach ($smallpic as $v) {
			unlink($v);
		}
		//删除中图
		$middlepic = explode(',', $dataDetail['middlepic']);
		foreach ($middlepic as $v) {
			unlink($v);
		}
		//删除大图
		$bigpic = explode(',', $dataDetail['bigpic']);
		foreach ($bigpic as $v) {
			unlink($v);
		}
		//删除数据库记录
		K('GoodsDetail')->where("goods_gid={$gid}")->delete();
		//删除货品列表
		K('GoodsList')->where("goods_gid={$gid}")->delete();
		
	}
	
	/**
	 * 异步获取类型属性
	 */
	public function getAttrSpec(){
		//验证是否是ajax请求
		if(!IS_AJAX) $this->error('非法请求');
		$tid = Q('post.tid',0,'intval');
		//获取类型属性数据
		$data = K('TypeAttr')->where("Type_tid={$tid}")->all();
		//将属性值转化为数组
		foreach($data as $k => $v){
			$data[$k]['tavalue'] = explode(',', $v['tavalue']);
		}
		echo json_encode($data);die;

	}
	
	
	/**
	 * 上传插件 处理
	 */
	public function upload(){
	     $upload = new Upload('Upload/Content/' . date('y/m'));
		 $file = $upload->upload();
		 if (empty($file)) {
		 	$this->ajax('上传失败');
		 } else {
		 	$data = $file[0];
			$this->ajax($data);
		 }
	}
	
	/**
	 * 删除图片
	 */
	public function delImg(){
		if(IS_AJAX) $this->error('非法请求');
		$path = Q('post.path');
		unlink($path);
		
		
	}
	
	
	
	
}





















 ?>